package com.goat.train.member.service;

import com.goat.train.common.exception.BusinessException;
import com.goat.train.common.respond.CommonResp;
import com.goat.train.common.util.SnowUtil;
import com.goat.train.member.domain.Member;
import com.goat.train.member.domain.MemberExample;
import com.goat.train.member.mapper.MemberMapper;
import com.goat.train.member.req.MemberRegisterReq;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import static com.goat.train.common.exception.BusinessExceptionEnum.MEMBER_MOBILE_EXIST;

@Service
public class MemberService {

    @Resource
    private MemberMapper memberMapper;

    public Long test() {
        return memberMapper.countByExample(null);
    }


    public CommonResp<Long> register(MemberRegisterReq req) {
        //先判断手机号是否已经注册过
        MemberExample example = new MemberExample();
        example.createCriteria().andMobileEqualTo(req.getMobile());
        long count = memberMapper.countByExample(example);
        if (count > 0) {
            throw new BusinessException(MEMBER_MOBILE_EXIST);
        }

        Member member = new Member();
        member.setId(SnowUtil.nextId());
        member.setMobile(req.getMobile());
        memberMapper.insert(member);
        return CommonResp.success(member.getId());
    }
}
